package servlet;

import dao.CardDaoMapper;
import entity.Card;
import org.apache.ibatis.session.SqlSession;
import service.CardService;
import utils.MyBatisUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/ListServlet")
public class ListServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        CardService service = new CardService();
        int pageNo = 1;
        int pageSize = 3;
        if (request.getParameter("pageNo") != null){
            pageNo = Integer.parseInt(request.getParameter("pageNo"));
        }
        System.out.println(request.getParameter("pageNo"));
        System.out.println(pageNo);
        int pageIndex = (pageNo-1) * pageSize;

        List<Card> list = service.list(pageIndex,pageSize);
        request.setAttribute("pageNo",pageNo);
        request.setAttribute("list",list);
        request.getRequestDispatcher("index.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
